package com.example.demo.chat;

import com.sun.corba.se.internal.Interceptors.PIORB;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * 网状服务器初始化
 *
 * @author JHY
 * @date 2023/03/08
 */
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 通过SocketChannel获取对应的管道
        ChannelPipeline pipeline = channel.pipeline();
        // 通过管道添加Handler
        // HttpServerCodec由Netty提供的助手类,可以理解为拦截器
        // 当请求到服务器需要解码,响应到客户端需要编码
        pipeline.addLast("HttpServerCodec", new HttpServerCodec());
        // 写大数据流支持
        pipeline.addLast("ChunkedWriteHandler", new ChunkedWriteHandler());
        // 对httpMessage 聚合处理
        pipeline.addLast("HttpObjectAggregator", new HttpObjectAggregator(1024 * 64));
        pipeline.addLast("WebSocketServerProtocolHandler", new WebSocketServerProtocolHandler("/"));
        // 添加自定义助手类
        pipeline.addLast("WebSocketHandler", new WebSocketHandler());


    }


}